/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin		 	*/
/*	Project:	GlbRtbEng-1.0.0			*/
/*	Author:		gong_libin			*/
/*	Date:		2016_06_26			*/
/*	File:		GlbRtbAdvsTool.cpp		*/
/********************************************************/

#include <getopt.h>

#include "GlbRtbAdvs.h"

int main(int argc, char* argv[])
{
 	int iIndex = 0;
	int iReturn = 0;
	CGlbRtbAdvs CAdvs;
	struct option stOpt[] = {
		{"reset", 0, 0, 'r'},
		{"print", 0, 0, 'p'},
		{"delete", 0, 0, 'd'},
		{"create", 1, 0, 'c'},
		{"select", 1, 0, 's'},
		{"version", 0, 0, 'v'},
		{"help", 0, 0, 'h'},
		{0, 0, 0, 0},
	};

	if (GLB_SUCCESS == CAdvs.GlbRtbBaseCreate(ftok(GLB_RA_PATH, GLB_RA_KEY), GLB_RA_SUM, GLB_RA_SIZE, GlbRtbAdvsComp)) {
		CAdvs.GlbRtbBaseCount();
		while (GLB_FAILURE != (iReturn = getopt_long(argc, argv, "rpdc:s:vh", stOpt, &iIndex))) {
			switch (iReturn) {
				case 'r':
					CAdvs.GlbRtbBaseReset(GLB_RA_SUM);
					break;
				case 'p':
					CAdvs.GlbRtbAdvsDisplay();
					break;
				case 'd':
					CAdvs.GlbRtbBaseDelete();
					break;
				case 'c':
					CAdvs.GlbRtbBaseReset(GLB_RA_SUM);
					CAdvs.GlbRtbBaseInput((void*)optarg);
					break;
				case 's':
					GLBRAINFO_S stKey, stVal;
					strncpy(stKey.m_szAdId, optarg, GLB_BYTE128);
					if (GLB_SUCCESS == CAdvs.GlbRtbBaseSearch(&stKey, &stVal)) {
						GlbRtbAdvsPrint(&stVal);
					}
					else {
						GLB_PRINT("%s -> NULL\n", optarg);
					}
					break;
				case 'v':
					GLB_PRINT("Copyright (C) 2016 Gong Li Bin	\n");
					GLB_PRINT("GlbRtbAdvsTool Build on %s %s.\n", __DATE__, __TIME__);
					return GLB_SUCCESS;
				case 'h':
					GLB_PRINT("Usage: %s [options]\n", argv[0]);
					GLB_PRINT("Valid options are:\n");
					GLB_PRINT("[0] -r or --reset:      reset memory\n");
					GLB_PRINT("[0] -p or --print:      print all advs\n");
					GLB_PRINT("[0] -c or --create:     create memory from advs\n");
					GLB_PRINT("[0] -s or --select:     select advs from memory\n");
					GLB_PRINT("[0] -d or --delete:     delete advs from memory\n");
					GLB_PRINT("Copyright (C) 2016 Gong Li Bin	\n");
					GLB_PRINT("GlbRtbAdvsTool Build on %s %s.\n", __DATE__, __TIME__);
					return GLB_SUCCESS;
				default:
					return GLB_SUCCESS;
			}
		}
	}
	else {
		GLB_ERROR("Failed to GlbRtbBaseCreate\n");
	}

	return GLB_SUCCESS;
}
